﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class controls_usrIdNumber : System.Web.UI.UserControl
{
    protected string strIdPrefix = "";
    protected string strIdPostfix = "";
    protected string strIdNumber = "";
    protected string strValidatinoGroup = "";
    protected string strClientScript = "";
    protected string strPrefixErrorMessage = "Choose ID number initial character.";
    protected string strIdNoErrorMessage = "NRIC/FIN number is not valid.";
    protected string strIdNoLengthErrorMessage = "ID number must be 7 digit.";
    protected string strIdNoRequireErrrorMessage = "Require ID number.";
    protected string strPostfixErrorMessage = "Choose ID number last character.";
    protected bool bEnabled = true;
    protected bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlIdPostfix.Attributes.Add("onchange", "ShowValidationEffectTwo('" + ddlIdPostfix.ClientID + "','" + csvIdPostfix.ClientID + "','" + csvIdNumber.ClientID + "');");
            ddlIdPrefix.Attributes.Add("onchange", strClientScript + "ShowValidationEffectTwo('" + ddlIdPrefix.ClientID + "','" + csvIdPrefix.ClientID + "','" + csvIdNumber.ClientID + "');");
            txtIdNumber.Attributes.Add("onblur", "ShowValidationEffectThree('" + txtIdNumber.ClientID + "','" + rfvIdNumber.ClientID + "','" + csvIdNumber.ClientID + "','" + revIdNumberLength.ClientID + "');");
        }
    }

    public void ChangeValidateEffectPrefix(bool bChange)
    {
        if (bChange)
        {
            ddlIdPrefix.ControlStyle.CssClass = "validationError";
        }
        else
        {
            ddlIdPrefix.ControlStyle.CssClass = "";
        }
    }

    public void ChangeValidateEffect(bool bChange)
    {
        if (bChange)
        {
            ddlIdPrefix.ControlStyle.CssClass = "validationError";
            txtIdNumber.ControlStyle.CssClass = "validationError";
            ddlIdPostfix.ControlStyle.CssClass = "validationError";
        }
        else
        {
            ddlIdPrefix.ControlStyle.CssClass = "";
            txtIdNumber.ControlStyle.CssClass = "";
            ddlIdPostfix.ControlStyle.CssClass = "";
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (csvIdPrefix.Enabled && csvIdPrefix.ValidationGroup == strValidationGroup)
        {
            csvIdPrefix.Validate();
            if (csvIdPrefix.IsValid == false)
            {
                ddlIdPrefix.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlIdPrefix.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlIdPrefix.ControlStyle.CssClass = "";
        }

        if (rfvIdNumber.Enabled && rfvIdNumber.ValidationGroup == strValidationGroup)
        {
            rfvIdNumber.Validate();
            if (rfvIdNumber.IsValid == false)
            {
                txtIdNumber.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtIdNumber.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtIdNumber.ControlStyle.CssClass = "";
        }

        if (revIdNumberLength.Enabled && revIdNumberLength.ValidationGroup == strValidationGroup)
        {
            if (rfvIdNumber.IsValid)
            {
                revIdNumberLength.Validate();
                if (revIdNumberLength.IsValid == false)
                {
                    txtIdNumber.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtIdNumber.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtIdNumber.ControlStyle.CssClass = "";
        }

        if (csvIdNumber.Enabled && csvIdNumber.ValidationGroup == strValidationGroup)
        {
            if (rfvIdNumber.IsValid && revIdNumberLength.IsValid)
            {
                csvIdNumber.Validate();
                if (csvIdNumber.IsValid == false)
                {
                    txtIdNumber.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtIdNumber.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtIdNumber.ControlStyle.CssClass = "";
        }

        if (csvIdPostfix.Enabled && csvIdPostfix.ValidationGroup == strValidationGroup)
        {
            if (csvIdPrefix.IsValid && csvIdNumber.IsValid)
            {
                csvIdPostfix.Validate();
                if (csvIdPostfix.IsValid == false)
                {
                    ddlIdPostfix.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    ddlIdPostfix.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            ddlIdPostfix.ControlStyle.CssClass = "";
        }
    }

    private void GetXmlDatasource()
    {
        try
        {
            XmlDataSource xdsIdPrefix = new XmlDataSource();
            xdsIdPrefix.DataFile = "~/controls/IdNumberPrefix.xml";
            xdsIdPrefix.GetXmlDocument();
            ddlIdPrefix.DataSource = xdsIdPrefix;
            ddlIdPrefix.DataTextField = "name";
            ddlIdPrefix.DataValueField = "value";
            ddlIdPrefix.DataBind();

            XmlDataSource xdsIdPostfix = new XmlDataSource();
            xdsIdPostfix.DataFile = "~/controls/IdNumberPostfix.xml";
            xdsIdPostfix.GetXmlDocument();
            ddlIdPostfix.DataSource = xdsIdPostfix;
            ddlIdPostfix.DataTextField = "name";
            ddlIdPostfix.DataValueField = "value";
            ddlIdPostfix.DataBind();

        }
        catch (Exception Ex)
        {

        }
    }
    protected void IdPrefixServerValidation(object source, ServerValidateEventArgs args)
    {
        try
        {
            // Test whether the value is -1
            if (args.Value != "-1")
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    protected void IdPostfixServerValidation(object source, ServerValidateEventArgs args)
    {
        try
        {
            // Test whether the value is -1
            if (args.Value != "-1")
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    //protected void IdNumberLengthServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    try
    //    {
    //        // Test whether the lenght of value is < 7.
    //        args.IsValid = !(args.Value.Length < 7);
    //    }

    //    catch (Exception ex)
    //    {
    //        args.IsValid = false;
    //    }
    //}

    protected void CheckIdNumberServerValidation(object source, ServerValidateEventArgs args)
    {
        try
        {
            // Test whether the lenght of value is < 7.
            //Weight        2  7  6  5  4  3  2 
            //Check Digit   1  2  3  4  5  6  7  8  9  10  11 
            //Alphabet      A  B  C  D  E  F  G  H  I   Z   J 

            /*
             * 1) Take for example I want to test the NRIC number S1234567. The first digit you multiply by 2, second multiply by 7, third by 6, fourth by 5, fifth by 4, sixth by 3, seventh by 2. Then you add the totals together. So,1×2+2×7+3×6+4×5+5×4+6×3+7×2=106.

                2) If the first letter of the NRIC starts with T or G, add 4 to the total.

                3) Then you divide the number by 11 and get the remainder. 106/11=9r7

                4) You can get the alphabet depending on the IC type (the first letter in the IC) using the code below:
                If the IC starts with S or T: 0=J, 1=Z, 2=I, 3=H, 4=G, 5=F, 6=E, 7=D, 8=C, 9=B, 10=A
                If the IC starts with F or G: 0=X, 1=W, 2=U, 3=T, 4=R, 5=Q, 6=P, 7=N, 8=M, 9=L, 10=K
             */


            if (args.Value.Length == 7)
            {
                if (args.Value.Length > 0 && args.Value.Length == 7)
                {
                    string strTempIDNumber = args.Value.ToString();
                    int D1 = Convert.ToInt32(strTempIDNumber.Substring(0, 1));
                    int D2 = Convert.ToInt32(strTempIDNumber.Substring(1, 1));
                    int D3 = Convert.ToInt32(strTempIDNumber.Substring(2, 1));
                    int D4 = Convert.ToInt32(strTempIDNumber.Substring(3, 1));
                    int D5 = Convert.ToInt32(strTempIDNumber.Substring(4, 1));
                    int D6 = Convert.ToInt32(strTempIDNumber.Substring(5, 1));
                    int D7 = Convert.ToInt32(strTempIDNumber.Substring(6, 1));

                    int SunDigit = 0;
                    int ModDigit = 0;
                    int ResultDigit = 0;
                    // Zero = 0 positon dummy 
                    //char[] resultArrChar = { '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'Z', 'J' };

                    char[] STArrChar = { 'J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' };
                    char[] FGArrChar = { 'X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K' };

                    SunDigit = (2 * D1) + (7 * D2) + (6 * D3) + (5 * D4) + (4 * D5) + (3 * D6) + (2 * D7);
                    ModDigit = SunDigit % 11;

                    if (ddlIdPrefix.Text == "T" || ddlIdPrefix.Text == "G")
                    {
                        ResultDigit = ModDigit + 4;
                    }
                    else
                    {
                        ResultDigit = ModDigit;
                    }

                    if (ResultDigit > 10)
                    {
                        //Eg. G6091872X
                        ResultDigit = ResultDigit % 11;
                    }

                    char resultChar = '0';

                    if (ddlIdPrefix.Text == "S" || ddlIdPrefix.Text == "T")
                    {
                        resultChar = STArrChar[ResultDigit];
                    }
                    else if (ddlIdPrefix.Text == "F" || ddlIdPrefix.Text == "G")
                    {
                        resultChar = FGArrChar[ResultDigit];
                    }

                    if (ddlIdPostfix.Text == resultChar.ToString())
                    {
                        args.IsValid = true;
                    }
                    else
                    {
                        args.IsValid = false;
                        //csvIdNumber.ErrorMessage = "";
                        //csvIdNumber.ErrorMessage += "Sun Digit " + SunDigit.ToString() + "<br />";
                        //csvIdNumber.ErrorMessage += "Mod Digit " + ModDigit.ToString() + "<br />";
                        //csvIdNumber.ErrorMessage += "Result Char" + resultChar.ToString() + "<br />";

                    }
                }
                else
                {
                    args.IsValid = false;
                }
            }
            else
            {
                args.IsValid = false;
            }
        }

        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            SetReadOnly();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvIdNumber.Enabled = bValue;
        csvIdNumber.Enabled = bValue;
        revIdNumberLength.Enabled = bValue;
        csvIdPostfix.Enabled = bValue;
        csvIdPrefix.Enabled = bValue;
    }

    public string ClientValidationScript
    {
        get { return strClientScript; }
        set { strClientScript = value; }
    }

    public string Prefix
    {
        get { return strIdPrefix; }
        set { strIdPrefix = value; }
    }

    public string Postfix
    {
        get { return strIdPostfix; }
        set { strIdPostfix = value; }
    }

    public string IDNumber
    {
        get { return strIdNumber; }
        set { strIdNumber = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string PrefixErrorMessage
    {
        get { return strPrefixErrorMessage; }
        set { strPrefixErrorMessage = value; }
    }

    public string PostfixErrorMessage
    {
        get { return strPostfixErrorMessage; }
        set { strPostfixErrorMessage = value; }
    }


    public string IdNoErrorMessage
    {
        get { return strIdNoErrorMessage; }
        set { strIdNoErrorMessage = value; }
    }

    public string IdNoRequireErrorMessage
    {
        get { return strIdNoRequireErrrorMessage; }
        set { strIdNoRequireErrrorMessage = value; }
    }

    public string IdNoLengthErrorMessage
    {
        get { return strIdNoLengthErrorMessage; }
        set { strIdNoLengthErrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlGender.SelectedIndex = 0;
        ddlIdPrefix.Enabled = bEnabled;
        ddlIdPostfix.Enabled = bEnabled;
        txtIdNumber.Enabled = bEnabled;

        csvIdPrefix.Enabled = bEnabled;
        csvIdPostfix.Enabled = bEnabled;
        csvIdNumber.Enabled = bEnabled;
        rfvIdNumber.Enabled = bEnabled;
        revIdNumberLength.Enabled = bEnabled;
    }

    private void SetReadOnly()
    {
        //for (int i = 0; i < ddlIdPrefix.Items.Count; i++)
        //{
        //    if (ddlIdPrefix.Items[i].Selected == false)
        //    {
        //        ddlIdPrefix.Items[i].Enabled = !bReadOnly;
        //    }
        //}
        if (bReadOnly)
        {
            ddlIdPrefix.Text = strIdPrefix;
            ddlIdPostfix.Text = strIdPostfix;

            string orgPrefixText = ddlIdPrefix.SelectedItem.Text;
            string orgPrefixValue = ddlIdPrefix.SelectedItem.Value;

            string orgPostfixText = ddlIdPostfix.SelectedItem.Text;
            string orgPostfixValue = ddlIdPostfix.SelectedItem.Value;

            ddlIdPrefix.Items.Clear();
            ddlIdPostfix.Items.Clear();
            ddlIdPrefix.Items.Add(new ListItem(orgPrefixText, orgPrefixValue));
            ddlIdPostfix.Items.Add(new ListItem(orgPostfixText, orgPostfixValue));

            txtIdNumber.ReadOnly = bReadOnly;
        }
        //for (int i = 0; i < ddlIdPostfix.Items.Count; i++)
        //{
        //    if (ddlIdPostfix.Items[i].Selected == false)
        //    {
        //        ddlIdPostfix.Items[i].Enabled = !bReadOnly;
        //    }
        //}
    }

    protected override void OnPreRender(EventArgs e)
    {
        //SetEnabled();

        if (strIdPrefix != string.Empty)
        {
            ddlIdPrefix.Text = strIdPrefix;
        }

        if (strIdPostfix != string.Empty)
        {
            ddlIdPostfix.Text = strIdPostfix;
        }

        if (strIdNumber != string.Empty)
        {
            txtIdNumber.Text = strIdNumber;
        }


        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
            {
                csvIdPrefix.ValidationGroup = strValidatinoGroup;
                csvIdPostfix.ValidationGroup = strValidatinoGroup;
                revIdNumberLength.ValidationGroup = strValidatinoGroup;
                csvIdNumber.ValidationGroup = strValidatinoGroup;
                rfvIdNumber.ValidationGroup = strValidatinoGroup;
            }
        }


        if (strPrefixErrorMessage != string.Empty)
        {
            //csvIdPrefix.ErrorMessage = strPrefixErrorMessage;
        }

        if (strPostfixErrorMessage != string.Empty)
        {
            //csvIdPostfix.ErrorMessage = strPostfixErrorMessage;
        }

        if (strIdNoLengthErrorMessage != string.Empty)
        {
            //revIdNumberLength.ErrorMessage = strIdNoLengthErrorMessage;
        }

        if (strIdNoErrorMessage != string.Empty)
        {
            //csvIdNumber.ErrorMessage = strIdNoErrorMessage;
        }

        if (strIdNoRequireErrrorMessage != string.Empty)
        {
            //rfvIdNumber.ErrorMessage = strIdNoRequireErrrorMessage;
        }
    }

    public void SetProperty()
    {
        strIdPrefix = ddlIdPrefix.SelectedValue;
        strIdNumber = txtIdNumber.Text;
        strIdPostfix = ddlIdPostfix.SelectedValue;
    }

    public string GetIdNumber()
    {
        strIdPrefix = ddlIdPrefix.SelectedValue;
        strIdNumber = txtIdNumber.Text;
        strIdPostfix = ddlIdPostfix.SelectedValue;
        string _idno = strIdPrefix + strIdNumber + strIdPostfix;

        return _idno;
    }

    public string GetIdPrefix()
    {
        return ddlIdPrefix.Text;
    }

    public bool IsValidePrefixRequirement()
    {
        csvIdPrefix.Validate();
        return csvIdPrefix.IsValid;
    }
}
